package com.zhiyou.app.shop.website.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Component;

import com.zhiyou.app.shop.manager.dao.impl.BaseHibernateDAO;
import com.zhiyou.app.shop.website.dao.IUserInfoDao;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.dao.pojo.UserAccount;

@Component
public class UserInfoDaoImpl extends BaseHibernateDAO<User, String> implements IUserInfoDao {

	public UserInfoDaoImpl(){
		super(User.class);
	}

	public User findByAccount(String account) {
		List<User> list = findByProperty("userAccount", account);
		if(list != null && list.size() >0 ){
			return list.get(0);
		}
		return null;
	}

	public List<User> findByRecommendId(final String account) {
		final String hql = "from User where recommendId=:recommendId and date_format(activityDate,'%Y-%m-%d')<='2013-12-02' ";
		final Map<String, Object> param = new HashMap<String, Object>();
		param.put("recommendId", account);
		List<User> list = hibernateTemplate.execute(new HibernateCallback<List<User>>(){
			public List<User> doInHibernate(Session session)throws HibernateException {
				Query query = null;
				try {
					query = session.createQuery(hql);
				} catch (Exception e) {
					e.printStackTrace();
				}
				query.setProperties(param);
				List<User> listu = query.list();
				if(listu != null && listu.size() > 0){
					return listu;
				}
				return null;
			}			
		});
		return list;
	}
	
}
